var entity = require('../entity');
var Card = entity.Card;


exports.newAndSave = function(user, cardparams, callback){
   var card = new Card();
    card.name = cardparams.name || user.name;
    card.userId = user.id || user._id;
    card.company = cardparams.company || '';
    card.position = cardparams.position || '';
    card.address = cardparams.address || user.country+user.province+user.city;
    card.mobile = cardparams.mobile || '';
    card.email = cardparams.email || '';
    card.qq = cardparams.qq || '';
    card.business = cardparams.business || '';
    card.product = cardparams.product || '';
    card.gender = user.gender;
    card.save(function(err, card){
        callback(err, card)
    });
}

exports.findCardById = function(id, callback){
    Card.findOne({_id: id}).exec(function(err, card){
        callback(err, card);
    })
}

exports.updateCard = function(id, params, callback){
    Card.update({_id: id}, {$set: {
        name: params.name,
        company: params.company,
        position: params.position,
        mobile: params.mobile,
        email: params.email,
        business: params.business,
        address: params.address,
        product: params.product
    }}, {}, function(err, card){
        callback(err, card)
    })
}

exports.setCardColumePermission = function(permission, card, callback){
    permission.showMobile || (card.mobile? card.mobile= card.mobile.substr(0,3)+' * * * * * * * *':card.mobile='');
    permission.showCompany || (card.company = '');
    permission.showPosition || (card.position = '');
    permission.showBusiness || (card.business = '');
    permission.showProduct || (card.product = '');
    permission.showEmail || (card.email = '');
    callback(card);
}